package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.CustomerLinkmen;
import cn.wolfcode.car.business.domain.CustomerVisit;
import cn.wolfcode.car.business.query.CustomerVisitQuery;
import cn.wolfcode.car.business.service.ICustomerService;
import cn.wolfcode.car.business.service.ICustomerVisitService;
import cn.wolfcode.car.business.service.ILinkmenService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/customerVisit")
public class CustomerVisitController {
    //模板前缀
    private static final String prefix = "business/customerVisit/";

    @Autowired
    private ICustomerVisitService customerVisitService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ILinkmenService linkmenService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:customerVisit:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("business:customerVisit:add")
    @RequestMapping("/addPage")
    public String addPage(Model model){
        //设置时间格式为yyyy-MM-dd,将当前时间放到作用域中(做默认时间)
        Date date = new Date();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateformat.format(date);
        model.addAttribute("data", format);
        //将Linkmen数据查询出来,放到作用域中(做数据回显)
        model.addAttribute("Linkmen", linkmenService.list());
        //将customer数据查询出来,放到作用域中(做数据回显)
        model.addAttribute("customer",customerService.list());
        return prefix + "add";
    }

    //新增
    @RequiresPermissions("business:customerVisit:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CustomerVisit customerVisit){
        //设置录入时间
        customerVisit.setInputTime(new Date());
        //将数据保存到数据库中
        customerVisitService.save(customerVisit);
        return AjaxResult.success();
    }

    //列表
    @RequiresPermissions("business:customerVisit:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CustomerVisit> query(CustomerVisitQuery qo){
        return customerVisitService.query(qo);
    }

    //获取对应联系人 /getLinkman
    @RequestMapping("/getLinkman")
    @ResponseBody
    public AjaxResult getLinkman(Long id){
        List<CustomerLinkmen> linkmens = linkmenService.selectByCustomerId(id);
        return AjaxResult.success(linkmens);
    }

}
